package com.audaque.springboot.foshanupload.core.component;

import com.google.common.util.concurrent.RateLimiter;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zgb
 * @desc ...
 * @date 2022-01-31 00:22:30
 */
@Component
public class GuauaRateLimiterProvider {

    private ConcurrentHashMap<String, RateLimiter> rateLimiterConcurrentHashMap = new ConcurrentHashMap<>();

    public  synchronized RateLimiter getRateLimiter(String name,double token){
        //判断该名称是否有创建rateLimiter 没有则创建,有则拿来用
        RateLimiter rateLimiter = rateLimiterConcurrentHashMap.get(name);
        if (rateLimiter == null) {
            rateLimiterConcurrentHashMap.put(name, RateLimiter.create(token));
            rateLimiter = rateLimiterConcurrentHashMap.get(name);
        }
        return rateLimiter;
    }


}
